package com.example.deliveryapplication

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.deliveryapplication.entity.Plan
import com.example.deliveryapplication.entity.PlanWithPlanItem
import com.example.deliveryapplication.sql.PlanDao
import com.example.deliveryapplication.sql.PlanItemDao

class HistoryViewModel:BaseViewModel() {
    private val mGetPlanResult = MutableLiveData<List<PlanWithPlanItem>>()
    val getPlanResult: LiveData<List<PlanWithPlanItem>> get() = mGetPlanResult
    fun getPlan(planDao: PlanDao){
        launch {
            val clients = planDao.getPlansWithItemCount()
            mGetPlanResult.postValue(clients)
        }
    }


    private val mDeletePlanResult = MutableLiveData<Int>()
    val deletePlanResult: LiveData<Int> get() = mDeletePlanResult
    fun deletePlan(planDao: PlanDao, planItemDao: PlanItemDao, bean:Plan){
        launch {
            val list=planItemDao.loadAllChart(bean.id)
            planItemDao.deleteByPlanId(list)
            val result = planDao.deletePlan(bean)
            mDeletePlanResult.postValue(result)
        }
    }
}